package com.csun.cmny.provider.controller;

import com.csun.cmny.core.annotation.ValidateAnnotation;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.model.domain.OmcChannel;
import com.csun.cmny.provider.model.domain.OmcChannels;
import com.csun.cmny.provider.model.domain.OmcFollowUp;
import com.csun.cmny.provider.model.domain.OmcVisitor;
import com.csun.cmny.provider.model.dto.channel.channelSaveDto;
import com.csun.cmny.provider.model.dto.channels.ChannelsQueryDto;
import com.csun.cmny.provider.model.dto.channels.channelsSaveDto;
import com.csun.cmny.provider.model.dto.channels.channelsUpdateDto;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.OmcChannelsService;
import com.csun.cmny.provider.service.OmcVisitorService;
import com.csun.cmny.util.PublicUtil;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping(value = "/channels", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class OmcChannelsController extends BaseController {
    @Resource
    private CmnyMessageSource cmnyMessageSource;
    @Resource
    private OmcChannelsService omcChannelsService;
    @Resource
    private OmcVisitorService omcVisitorService;

    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "新增渠道")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Wrapper addChanner(@RequestBody @Valid channelsSaveDto channelsSaveDto, BindingResult br) {

        OmcChannels channels = new OmcChannels();
        channels.setChannelName(channelsSaveDto.getChannelName());
        channels.setGroupId(getLoginAuthDto().getGroupId());
        List<OmcChannels> select = omcChannelsService.select(channels);
        if(PublicUtil.isNotEmpty(select)){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,cmnyMessageSource.getMessage("channel.name.repeat", null));
        }

        OmcChannels omcChannels = new OmcChannels();
        BeanUtils.copyProperties(channelsSaveDto,omcChannels);
        omcChannels.setGroupId(getLoginAuthDto().getGroupId());

        int relust = omcChannelsService.save(omcChannels);


        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),relust);
    }

    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "修改渠道")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Wrapper updateChanner(@RequestBody @Valid channelsUpdateDto channelsUpdateDto, BindingResult br) {

        OmcChannels channels = new OmcChannels();
        channels.setChannelName(channelsUpdateDto.getChannelName());
        channels.setGroupId(getLoginAuthDto().getGroupId());
        List<OmcChannels> select = omcChannelsService.select(channels);

        if(PublicUtil.isNotEmpty(select)&&!select.get(0).getId().equals(channelsUpdateDto.getId())){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,cmnyMessageSource.getMessage("channel.name.repeat", null));
        }

        OmcChannels omcChannels = new OmcChannels();
        BeanUtils.copyProperties(channelsUpdateDto,omcChannels);

        int relust = omcChannelsService.update(omcChannels);


        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),relust);
    }

    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "删除渠道")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public Wrapper deleteChanner(Long id) {

        OmcVisitor visitor = new OmcVisitor();
        visitor.setChannelId(id);
        List<OmcVisitor> select = omcVisitorService.select(visitor);

        if(PublicUtil.isNotEmpty(select)){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,cmnyMessageSource.getMessage("channel.delete.error", null));
        }


        int relust = omcChannelsService.deleteByKey(id);


        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),relust);
    }

    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "分页查询渠道")
    @RequestMapping(value = "/select", method = RequestMethod.POST)
    public Wrapper select(@RequestBody @Valid ChannelsQueryDto channelsQueryDto) {

        channelsQueryDto.setGroupId(getLoginAuthDto().getGroupId());
        List<OmcChannels>list=omcChannelsService.selectChannels(channelsQueryDto);


        PageInfo<OmcChannels> pageInfo = new PageInfo<>(list);


        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),pageInfo);
    }
}
